import json
from web.common.excel_util import ExcelUtil

# 满足定制化需求
def read_excel_data(xl_path, sheet_index_or_name = None, row_num = None, *cols_index):
    """
        如果不指定获取某一sheet页的数据，就会获取所有sheet页的数据，返回的值为字典;
        其中字典的key为sheet页的名称或索引值，字典的value为一个列表，
        并且这个列表中每一个元素为sheet页表格中每一行数据组成的子列表

        如果指定获取某一sheet页的数据，则返回的值为列表；

        并且满足定制化需求，可以读取全部的数据，也可只读取某一个sheet页的数据，
        也可只读取某个sheet页的某一行数据，并且满足只返回sheet页的某几列数据
        :param xl_path: xls文件路径
        :param sheet_index_or_name: sheet页的索引或名称
        :param row_num: 获取sheet表格某一行的数据
        :param cols_index: 获取sheet表格某一列或几列的数据
        :return: 返回字典或列表
    """

    xlUtils = ExcelUtil(xl_path)
    xl_dict = {}
    cell_list = []

    if sheet_index_or_name is not None and row_num is not None:
        cell_list.append(__get_cell_value(xlUtils, sheet_index_or_name, row_num, *cols_index))
    elif sheet_index_or_name is not None and row_num is None:
        for row_num in range(1, xlUtils.get_rows_num(sheet_index_or_name)):
            cell_list.append(__get_cell_value(xlUtils, sheet_index_or_name, row_num, *cols_index))
    elif sheet_index_or_name is None and row_num is not None:
        for sheet_index_or_name in range(0, len(xlUtils.get_sheet_names())):
            cell_list = []
            cell_list.append(__get_cell_value(xlUtils, sheet_index_or_name, row_num, *cols_index))
            xl_dict[sheet_index_or_name] = cell_list
    elif sheet_index_or_name is None and row_num is None:
        for sheet_index_or_name in range(0, len(xlUtils.get_sheet_names())):
            cell_list = []
            for row_num in range(1, xlUtils.get_rows_num(sheet_index_or_name)):
                cell_list.append(__get_cell_value(xlUtils, sheet_index_or_name, row_num, *cols_index))
                if len(cell_list) != 0:
                    xl_dict[sheet_index_or_name] = cell_list
    if not xl_dict:
        if len(cell_list) != 0:
            xl_dict[sheet_index_or_name] = cell_list
    if sheet_index_or_name != None:
        return xl_dict[sheet_index_or_name]
    else:
        return xl_dict

def __get_cell_value(xlUtils:ExcelUtil, sheet_index_or_name, row_num,*cols_index):
    row_data = []
    if cols_index:
        for cell_cols_index in cols_index:
            row_data.append(xlUtils.get_cell_value(sheet_index_or_name, row_num, cell_cols_index))
    else:
        colsNum = xlUtils.get_cols_num(sheet_index_or_name)
        for cell_cols_index in range(0, colsNum):
            row_data.append(xlUtils.get_cell_value(sheet_index_or_name, row_num, cell_cols_index))
    return tuple(row_data)
a = read_excel_data('login_data.xls',0,None,1,2,3)
print(a)
# b = read_excel_data('login_data.xls',1)
# # print(b)
#
# def trans_data(dict_data:dict):
#     payload = {}
#     for K,V  in dict_data.items():
#         if type(V) is dict:
#             V = json.dumps(V, ensure_ascii=True)
#         payload[K] = V
#     return payload

#
# class RD_By():
#     CASE_NUM = 0
#     MOUDLE_NAME = 1
#     CASE_NAME = 2
#     REQUEST_METHOD = 3
#     REQUEST_URL = 4
#     REQUEST_HEADERS = 5
#     PARAMS_DESC = 6
#     REQUEST_DATA = 7
#     EXCEPT_RESULT_DESC = 8
#     EXCEPET_RESULT = 9